package com.lagou.edu.controller;

import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    ResumeService resumeService;

    @RequestMapping("/findAll")
    public String findAll(Model model) {
        List<Resume> resumes = resumeService.findAll();
        model.addAttribute("resumes", resumes);
        return "resume_list";
    }

    @RequestMapping("/findOne/{id}")
    public String findOne(Model model, @PathVariable("id") Long id) {
        Resume resume = resumeService.findById(id);
        model.addAttribute("resume", resume);
        return "edit";
    }

    @RequestMapping("/update")
    public String update(Model model, Resume resume) {
        System.out.println(resume);
        resumeService.update(resume);
        return "redirect:/resume/findAll";
    }

    @RequestMapping("/delete/{id}")
    public String delete(Model model, @PathVariable("id") Long id) {
        resumeService.deleteById(id);
        return "redirect:/resume/findAll";
    }

    @RequestMapping("/add")
    public String add(Model model, Resume resume) {
        return "add";
    }

    @RequestMapping("/addnew")
    public String addnew(Model model, Resume resume) {
        resumeService.insert(resume);
        return "redirect:/resume/findAll";
    }
}
